<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房屋编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['domain:appointment:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appointmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="预约房屋编号" align="center" prop="houseId"/>
      <el-table-column label="预约时间" align="center" prop="appTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否处理" align="center" prop="isHandle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_is_handle" :value="scope.row.isHandle"/>
        </template>
      </el-table-column>
      <el-table-column label="中介回复" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['domain:appointment:edit']"
            v-if="scope.row.isHandle != 2"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['domain:appointment:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleDetails(scope.row)"
            v-hasPermi="['domain:appointment:query']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleApp(scope.row)"
            v-hasPermi="['domain:appointment:handle']"
            v-if="scope.row.isHandle != 2"
          >处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--修改预约看房对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预约时间" prop="appTime">
          <el-date-picker v-model="form.appTime" type="datetime"
                          format="yyyy-MM-dd HH:mm:ss"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          :style="{width: '100%'}"
                          :picker-options="pickerOptions"
                          placeholder="请选择预约时间" clearable></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--查看房屋信息和用户信息对话框-->
    <el-dialog :title="title" :visible.sync="appDetail" width="500px" append-to-body>
      <el-form :model="detail" label-width="120px">
        <el-row>
          <el-col>
            <h4 class="el-form--inline">用户信息</h4>
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="detail.user.nickName" disabled/>
            </el-form-item>
            <el-form-item label="用户电话" prop="phone">
              <el-input v-model="detail.user.phone" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <h4 class="el-form--inline">房屋信息</h4>
            <el-form-item label="房屋名称" prop="houseName">
              <el-input v-model="detail.house.houseName" disabled/>
            </el-form-item>
            <el-form-item label="占地面积(㎡)" prop="houseArea">
              <el-input v-model="detail.house.houseArea" disabled/>
            </el-form-item>
            <el-form-item label="租金/月" prop="rentalPrice">
              <el-input v-model="detail.house.rentalPrice" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>


    <!-- 房屋中介处理预约看房对话框 -->
    <el-dialog :title="title" :visible.sync="handle" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="回复" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormHandle">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAppointment,
  getAppointment,
  delAppointment,
  addAppointment,
  updateAppointment, getAppointmentOthers, handleAppointment
} from "@/api/system/appointment";

export default {
  name: "Appointment",
  dicts: ['house_is_handle'],
  data() {
    return {
      //时间选择器
      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约看房表格数据
      appointmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //显示预约详情层
      appDetail: false,
      //处理预约看房对话框
      handle: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
      },
      // 表单参数
      form: {},
      //详情参数
      detail: {
        user: {},
        house: {}
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询预约看房列表 */
    getList() {
      this.loading = true;
      listAppointment(this.queryParams).then(response => {
        this.appointmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.appDetail = false;
      this.handle = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        appointmentId: null,
        userId: null,
        houseId: null,
        createTime: null,
        updateTime: null,
        status: "0",
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.appointmentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const appointmentId = row.appointmentId || this.ids
      getAppointment(appointmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约看房";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.appointmentId != null) {
            updateAppointment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 处理预约看房提交按钮 */
    submitFormHandle() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.appointmentId != null) {
            handleAppointment(this.form).then(response => {
              this.$modal.msgSuccess("处理成功");
              this.handle = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const appointmentIds = row.appointmentId || this.ids;
      this.$modal.confirm('是否确认删除预约看房编号为"' + appointmentIds + '"的数据项？').then(function () {
        return delAppointment(appointmentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 预约详情按钮,用于查询部分房屋信息和用户信息*/
    handleDetails(row) {
      getAppointmentOthers(row.userId, row.houseId).then(response => {
        this.detail.house = response.house;
        this.detail.user = response.user;
        this.appDetail = true;
        this.title = "详情信息";
      })
    },
    //中介处理预约看房申请
    handleApp(row) {
      this.reset();
      const appointmentId = row.appointmentId || this.ids
      getAppointment(appointmentId).then(response => {
        this.form = response.data;
        this.handle = true;
        this.title = "处理预约看房";
      });
    }
  }
};
</script>
